from django.utils.translation import gettext_lazy
from rest_framework.serializers import Serializer
from rest_framework.response import Response
from drf_yasg.utils import swagger_auto_schema

from apps.api.viewsets import NormalGenericViewSet
from apps.api.paginations import NewPageNumberPagination100
from utils.paginators import NoPaginatorInspector
from apps.app_global.configs_manager import global_configs


class MasterDomainViewSet(NormalGenericViewSet):
    permission_classes = []
    pagination_class = NewPageNumberPagination100
    lookup_field = 'id'

    @swagger_auto_schema(
        tags=['global'],
        operation_summary=gettext_lazy('列举主前端域名'),
        paginator_inspectors=[NoPaginatorInspector],
        responses={
            200: ''''''
        }
    )
    def list(self, request, *args, **kwargs):
        """
        列举主前端域名，不需身份验证

            http Code 200 Ok:
                {
                    "domains": [
                        "test.com",
                        "10.1.2.3"
                    ]
                }
        """
        domains_value = global_configs.get(global_configs.ConfigName.MASTER_YUNJING_HOSTS.value)
        domains = domains_value.splitlines()
        domains = [name.strip() for name in domains if name and name.strip()]
        return Response(data={
            'domains': domains
        })

    def get_serializer_class(self):
        return Serializer
